// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import login from '../pages/Login.vue'
import add from '../pages/Add.vue'
import list from '../pages/List.vue'
import report from '../pages/Report.vue'
import report02 from '../pages/Report02.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/add',
    name: 'add',
    component: add,
    meta:{
      needLogin:true
    }
  },
  {
    path: '/list',
    name: 'list',
    component: list,
    meta:{
      needLogin:true
    }
  },
  {
    path: '/report',
    name: 'report',
    component: report,
    meta:{
      needLogin:true,
      roles:["VIP","SVIP"]
    }
  },
  {
    path: '/report02',
    name: 'report02',
    component: report02,
    meta:{
      needLogin:true,
      roles:["SVIP"]
    }
  }
  
];

const router = createRouter({
  history: createWebHistory(),  // HTML5历史模式
  routes
});

//添加拦截器，控制权限
router.beforeEach((to, from, next) => {
  //alert("你想跳转从"+from.path+"--》"+to.path);
  let ss=sessionStorage.getItem("curUser");
  if(to.meta.needLogin&&ss==null){
    ElMessageBox.alert("请先登录",'提示')
    next("/login");
    return;
  }
  if(to.meta.roles){
     let user=JSON.parse(ss);
     if(to.meta.roles.includes(user.role)==false){
         ElMessageBox.alert("您无权限访问此页面，请升级为"+to.meta.roles,"警告")
         return;
         //next("/list")
     }
  }
  next();//放行
})

export default router;